<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace User\Controller;
use User\Controller\BaseController;

class IndexController extends BaseController {
    
    //个人中心
    public function index(){
        $map['user_id'] = session('user_id');
        $map['order_state'] = 30;
        $order = M('order')->where($map)->limit(3)->select();
        if($order){
            foreach ($order as $key => $value) {
                $order[$key]['order_goods'] = M('order_goods')->where('order_id='.$value['order_id'])->find();
            }
        }
        $this->assign('order', $order);
        
        //我的收藏
        $favorites = M('Favorites');
        $fav = $favorites->where(array('user_id'=>session('user_id'),'fav_type'=>20))->limit(8)->select();
        if($fav){
            foreach ($fav as $key => $value) {
                $fav[$key]['goods'] = M('goods')->where('goods_id='.$value['goods_id'])->find();
            }
        }
        $this->assign('fav', $fav);
        
        //新品
        $newGoods = M('goods')->where('goods_is_index=1')->order('goods_hits desc')->limit(2)->select();
        $this->assign('newGoods', $newGoods);
        //推荐
        $putGoods = M('goods')->where('goods_is_index=1')->order('goods_created desc')->limit(3)->select();
        $this->assign('putGoods', $putGoods);
        $this->display();
    }
    
    
    //个人信息
    public function information(){
        $user_id = session('user_id');
        
        if(IS_POST){
            $user_id = session('user_id');
            $datas = I('post.');
            // 生日拼接储存
            $arr['years'] = I('post.years') . '-';
            $arr['month'] = I('post.month') . '-';
            $arr['day'] = I('post.day');
            $datas['user_birthday'] = implode($arr);
            $Users = M('Users');
            $tf = $Users->where(array('user_id' => $user_id))->data($datas)->save();
            if ($tf !== null) {
                session('user_name', $datas['user_name']);
                $this->success('操作成功', U('information'));
            } else {
                $this->error('操作失败');
            }
        }
        $Users = M('Users');
        $res = $Users->where(array('user_id'=>$user_id))->find();
        $this->assign('res',$res);
        $date = explode("-",$res['user_birthday']);
        $this->assign('date',$date);
        $this->assign('active','information');
        $this->display();
    }
    
    
    public function safety(){
        $user_id = session('user_id');
        $user = M('users')->field('user_mobile,user_email')->where("user_id=".$user_id)->find();
        $this->assign('user',$user);
        $this->assign('active','safety');
        $this->display();
    }
    
    
    //修改密码
    public function password() {
        if (IS_POST) {
            $user_old_password = I('user_old_password');
            $user_new_password = I('user_new_password');
            $user_confirm_password = I('user_confirm_password');
            
            if ($user_new_password == $user_confirm_password) {
                $user_salt = M('users')->field('user_salt,user_passwd')->where('user_id='.session('user_id'))->getField('user_salt');
                list($passwords, $salt) = password($user_old_password, $user_salt['user_salt']);
                if ($passwords != $user_salt['user_passwd']) {
                    $this->error('输入的原密码不正确！');
                }else{
                    list($password, $salt) = password($user_confirm_password);
                    $dates['password'] = $password;
                    $dates['salt'] = $salt;
                    $where['user_id'] = session('user_id');
                    $re = M('users')->where($where)->save($dates);
                    if ($re !== FALSE) {
                        session('[destroy]');
                        $this->success('修改成功！');
                    } else {
                        $this->error('修改失败！');
                    }
                }
            }else{
                $this->error('两次输入的新密码不一致！');
            }
        }
        $this->assign('active','safety');
        $this->display();
    }

    //绑定手机
    public function bindphone(){
        if(IS_POST){
            $user_passwd = I('user_passwd');
            $user_new_phone = I('user_new_phone');
            $user_new_code = I('user_new_code');
            if($user_new_code == session('mobile_sms')){
                $user_salt = M('users')->field('user_salt,user_passwd')->where('user_id='.session('user_id'))->getField('user_salt');
                list($passwords, $salt) = password($user_passwd, $user_salt['user_salt']);
                if ($passwords != $user_salt['user_passwd']){
                    $this->error('输入的原密码不正确！');
                }else{
                    $where['user_id'] = session('user_id');
                    $data['user_mobile'] = $user_new_phone;
                    $ra = M('users')->where($data)->find();
                    if($ra){
                        $this->error('此手机号已被占用，请更换个手机号！');
                    }else{
                        $re = M('users')->where($where)->save($data);
                        if($re !==FALSE){
                            $this->success('手机号更换成功！');
                        }else{
                            $this->error('网络异常，请重试！');
                        }
                    }
                }
            }else{
                $this->error('手机验证码不正确！');
            } 
        }
        $user_id = session('user_id');
        $mobile = M('users')->where("user_id=".$user_id)->getField('user_mobile');
        $this->assign('mobile',$mobile);
        $this->assign('active','safety');
        $this->display();
    }
    
    //绑定邮箱
    public function bindemail(){
        if(IS_POST){
            $user_email = I('user_email');
            $user_code = I('user_code');
            if($user_code == session('email_code')){
                $where['user_id'] = session('user_id');
                $data['user_email'] = $user_email;
                $ra = M('users')->where($data)->find();
                if($ra){
                    $this->error('此邮箱已被绑定，请更换个邮箱！');
                }else{
                    $re = M('users')->where($where)->save($data);
                    if($re !==FALSE){
                        $this->success('邮箱更换成功！');
                    }else{
                        $this->error('网络异常，请重试！');
                    }
                }
            }else{
                $this->error('验证码不正确！');
            }
        }
        $this->assign('active','safety');
        $this->display();
    }
    
    //实名认证
    public function idcard(){
        if(IS_POST){
            $user_truename = I('user_truename');
            $user_card = I('user_card');
            $user_card_img = serialize(I('user_card_img'));
            $where['user_id'] = session('user_id');
            $data['user_truename'] = $user_truename;
            $data['user_card'] = $user_card;
            $data['user_card_img'] = $user_card_img;
            $data['user_card_bind'] = 2;
            $re = M('users')->where($where)->save($data);
            if($re){
                $this->success('实名认证已提交！请等待审核！');
            }else{
                $this->error('认证失败！');
            } 
        }
        $user_id = session('user_id');
        $mobile = M('users')->where("user_id=".$user_id)->getField('user_mobile');
        $this->assign('mobile',$mobile);
        $this->assign('active','safety');
        $this->display();
    }
    
}
